//
//  VisitorView.swift
//  Weibo
//
//  Created by 伍银荣 on 2021/12/15.
//  Copyright © 2021 com.WYR. All rights reserved.
//

import UIKit
import SnapKit

/////访客视图的协议
//protocol VisitorViewDelegate: NSObjectProtocol {
//    //定义代理方法
//    ///注册
//    func visitorViewDidRegister()
//    ///登录
//    func visitorViewDidLogin()
//}

class VisitorView: UIView {
    
//    //代理属性
//    //弱引用需要定义成可选项 和 var,不然对象一创建就被释放了
//    weak var delegate: VisitorViewDelegate?
//
//    //MARK: - 监听方法
//    @objc private func registerBtnClicked() {
//       delegate?.visitorViewDidRegister()
//    }
//
//    @objc private func loginBtnClicked() {
//        delegate?.visitorViewDidLogin()
//    }
    
    
    //MARK: - 设置视图信息
    func setupInfo(title: String, imageName: String?) {
        messageLabel.text = title
        //如果图片名称是 nil, 说明是首页,直接返回
        guard let imgName = imageName else {
            return
        }
        iconImageView.image = UIImage(named: imgName)
    }
    
    
    //initWithFrame 是 UIView 的指定构造函数
    //使用纯代码开发
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    //MARK: -懒加载控件
    //icon图片
    private lazy var iconImageView: UIImageView = UIImageView(imageName: "zhuzhugirl")

    //消息文字
    private lazy var messageLabel: UILabel = UILabel(title: "关注一些人,回来看看有什么惊喜关注一些人,回来看看有什么惊喜")
    
    //注册按钮
    lazy var registBtn: UIButton = UIButton (title: "注册", titleColor: .orange, btnBackgroundColor: .white)
    
    //登录按钮
    lazy var loginBtn: UIButton = UIButton(title: "登录", titleColor: .darkGray, btnBackgroundColor: .white)

    
    //必要初始化器
    //initWithCode - 使用xib 或者 storyBoard 开发加载的函数
    //使用 sb 开始的入口
    required init?(coder aDecoder: NSCoder) {
        // fatalError 阻止使用 storyBoard 使用当前自定义视图
        //如果只希望当前视图被纯代码的方式加载,可以使用 fatalError
        // fatalError 导致如果使用 SB 开发,调用这个视图,会直接崩溃
        fatalError("init(coder:) has not been implemented")
    }
    
}

//MARK: - 设置界面
extension VisitorView {
    private func setupUI() {
        //1. 添加控件
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registBtn)
        addSubview(loginBtn)
        
        //2.设置布局
        /**
         * 添加约束需要添加到父视图上
         * 建议: 子视图最好有一个统一的参照物
         
         NSLayoutConstraint(item: 视图,
         attribute: 约束属性,
         relatedBy: 约束关系,
         toItem: 参照视图,
         attribute: 参照属性,
         multiplier: 乘积,
         constant: 约束数值)
         
        */
        //translatesAutoresizingMaskIntoConstraints 默认是 true,支持使用 setFrame 的方式设置控件位置
        // false 支持使用自动布局来设置控件位置
//        for v in subviews {
//            v.translatesAutoresizingMaskIntoConstraints = false
//        }
//        messageLabel.translatesAutoresizingMaskIntoConstraints = false
//        registBtn.translatesAutoresizingMaskIntoConstraints = false
//        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        
        //icon图片
//        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: -60))
//        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 250))
//        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 250))
        
        iconImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
            make.width.height.equalTo(250)
        }
        
//        //1>消息文字
//        //距离 self 水平居中
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .top, relatedBy: .equal, toItem: iconImageView, attribute: .bottom, multiplier: 1.0, constant: 20))
//        //设置宽度
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 224))
//        //设置高度
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 36))
        
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.snp_centerX)
            make.top.equalTo(iconImageView.snp_bottom).offset(20)
            make.width.equalTo(224)
            make.height.equalTo(36)
        }
        
//
//        //注册按钮
//        addConstraint(NSLayoutConstraint(item: registBtn, attribute: .left, relatedBy: .equal, toItem: messageLabel, attribute: .left, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: registBtn, attribute: .top, relatedBy: .equal, toItem: messageLabel, attribute: .bottom, multiplier: 1.0, constant: 20))
//        addConstraint(NSLayoutConstraint(item: registBtn, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
//        addConstraint(NSLayoutConstraint(item: registBtn, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 36))
        registBtn.snp_makeConstraints { (make) in
            make.left.equalTo(messageLabel.snp_left)
            make.top.equalTo(messageLabel.snp_bottom).offset(20)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
        
//
//        //登录按钮
//        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .right, relatedBy: .equal, toItem: messageLabel, attribute: .right, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .top, relatedBy: .equal, toItem: messageLabel, attribute: .bottom, multiplier: 1.0, constant: 16))
//        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
//        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 36))
        
        loginBtn.snp_makeConstraints { (make) in
            make.right.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.width.height.top.equalTo(registBtn)
        }
        
        
        //设置背景图片
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        
        
    }
}
